package com.zzq.my.jetpacks.jectpackapp.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.bean.AccountInRecordInfo

/**
 * 描述：入账记录分组适配器
 * 作者：zhuangzeqin
 * 时间: 2022/9/11-15:27
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class AccountInRecordGroupAdapter(context: Context) : GroupedRecyclerViewAdapter(context) {
    private var mGroups: MutableList<AccountInRecordInfo> = ArrayList()

    fun getData(): MutableList<AccountInRecordInfo> {
        return mGroups
    }

    /**
     * 重新设置数据加载
     *
     * @param subDatas
     */
    @SuppressLint("NotifyDataSetChanged")
    fun setList(subData: List<AccountInRecordInfo>) {
        if (subData.isNotEmpty()) {
            mGroups.clear()
            mGroups.addAll(subData)
            notifyDataSetChanged()
        }
    }

    /**
     * 添加一个集合的数据
     *
     * @param subDatas
     */
    fun addAll(subData: List<AccountInRecordInfo>) {
        if (subData.isNotEmpty()) {
            val lastIndex = mGroups.lastIndex
            mGroups.addAll(subData)
//            notifyDataSetChanged()
            notifyItemRangeInserted(lastIndex, subData.size)
        }
    }

    override fun getGroupCount(): Int = mGroups.size

    override fun getChildrenCount(groupPosition: Int): Int {
        val childItemList = mGroups[groupPosition].childItemList
        return childItemList.size
    }

    override fun hasHeader(groupPosition: Int): Boolean = true

    override fun hasFooter(groupPosition: Int): Boolean = true

    override fun getHeaderLayout(viewType: Int): Int = R.layout.shop_incoming_item_record_in_head


    override fun getFooterLayout(viewType: Int): Int = R.layout.shop_incoming_item_no_data

    override fun getChildLayout(viewType: Int): Int = R.layout.shop_incoming_item_record_in_content

    /**
     * 头部
     */
    override fun onBindHeaderViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
        holder ?: return
        val item = mGroups[groupPosition]
        holder.setText(R.id.tv_date, item.date)
        holder.setText(R.id.tv_total, item.total)
        holder.setText(R.id.tv_num, item.count)
    }

    override fun onBindFooterViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int
    ) {
        holder ?: return

        if (mGroups[groupPosition].footerView) {
            holder.setVisible(R.id.ll_foot_view, View.VISIBLE)
        } else {
            holder.setVisible(R.id.ll_foot_view, View.GONE)
        }
    }

    /**
     * childView item
     */
    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        holder ?: return
        val childItem = mGroups[groupPosition].childItemList[childPosition]
        holder.setText(R.id.tv_title, childItem.itemTitle)
        holder.setText(R.id.tv_sub_date, childItem.itemDate)
        holder.setText(R.id.atv_amount, childItem.itemAmount)
    }

}